
#import "MyObject.h"

@implementation MyObject


//
//
// MemoPad,
// Par RenÝ Guimont
// Version 1,2 Mai 1994
// Version 1,3 janvier 1995
// 	Ajouter 2 blocnotes de plus (FTP, Stuff et Varia)
// Version 1,4a fÝvrier 1995
//    Ajout systÜme de calendrier,
//    correction cosmÝtique de l'application.
//
// Regardez dans le memu More Info pour plus de dÝtails sur cette 
// application.
// Look in the menu More Info for details of this application
//
// This software is ANYware
//
//

- appDidInit:sender
{


// Running empty defaults...
	memoFlag = OFF;
	voiceFlag = OFF;
	defaultPad = 0; 
	activePad = 0; 
	lastUsedPad = 0;       	


// Pour les services ces lignes sont essentielles!
	
	myListener = [NXApp appListener];
	[myListener setServicesDelegate: self];

// Setup pour les dÝfaults...

	if ((in = fopen("Library/MemoPad/memoDefaults", "r")) != NULL) {
		temp = fscanf(in,"%1d%1d%1d%1d%s%s", &memoFlag, &voiceFlag, &defaultPad, &lastUsedPad, memoSound, voiceSound );
		fclose(in);

	error = [templateTexte openRTFDFrom:"Library/MemoPad/template.rtfd"];

		[memoSoundPath setStringValue:memoSound];
		[voiceSoundPath setStringValue:voiceSound];

		if (voiceFlag == ON)		
			temp = SNDPlaySoundfile(voiceSound,6);
		}

	if (defaultPad == 9) 
		activePad = lastUsedPad;
	else
		activePad = defaultPad;


	[boutonMemo selectCellWithTag:memoFlag];						
	[boutonVoice selectCellWithTag:voiceFlag];
	[defaultPadSelector selectCellWithTag:defaultPad];
	[padSelector selectCellWithTag:activePad];

	tempPad = activePad;
	[self selectPadNumber:self];

	error = [mainWindow setFrameUsingName:"WindowFrameMemoPad"];
	[self loadPad:self];

	[self calculeDate:self];
	
	jourCourant = jourInt;
	moisCourant = moisInt;
	anneeCourant = anInt;
  
  	[self setMainCalendar:self];
  	[self findTodayDate:self];
	[self findTodayMemo:self]; 
	
	[yearSelector setNextText:yearSelector];

	[pad setSel:0:0]; 	// Installe un  curseur dans le scrollView
	[mainWindow orderFront:self];

	return self;
}

- appWillTerminate:sender
{
	[self savePad:self];
	[mainWindow saveFrameUsingName:"WindowFrameMemoPad"];

	if (defaultPad == 9)               // Last pad used flag...
		[self saveDefault:self];
	
	return self;

}

- setMainCalendar:sender
{	
	// Local Variables
	FILE *calCmd;
	char c, buff[512], *pbuff;
	
	sprintf( buff, "cal %d %d", moisInt, anneeInt );
	if ( (calCmd=popen(buff, "r")) == NULL )  {
		[calendrier setSel:0:0];		
		[calendrier replaceSel:"Can't build calendar!"];
		}
	else {
		pbuff = buff;
		while( (c=fgetc(calCmd)) != EOF )
			*(pbuff++)=c;
		*pbuff='\0';
		pclose(calCmd);
		[calendrier setSel:0:0];		
		[calendrier replaceSel:buff];
		}

	return self;
}


- findTodayDate:sender
{

	if ([calendrier findText:journee 
                 ignoreCase:YES 
                  backwards:NO 
                       wrap:YES] == YES);

	return self;	
}


- findTodayMemo:sender
{
	if ([pad findText:dateCourante
               ignoreCase:YES 
                backwards:NO 
                     wrap:YES] == YES) 
		if (memoFlag == ON)
			temp = SNDPlaySoundfile(memoSound,5);		

	return self;
}


- currentMonth:sender
{
	moisInt = moisCourant;
	anInt = anneeCourant;

	[self calendar:self];
	
	return self;
	
}


// This method builds the proper calendar in the window
- calendar:sender
{

	// Local Variables
	FILE *calCmd;
	char c, buff[512], *pbuff;
	int tempMonth, tempYear;
	
	sprintf( buff, "cal %d %d", moisInt, anneeInt );
	if ( (calCmd=popen(buff, "r")) == NULL )
		[calView setStringValue:"Can't build calendar!"];
	else {
		pbuff = buff;
		while( (c=fgetc(calCmd)) != EOF )
			*(pbuff++)=c;
		*pbuff='\0';
		pclose(calCmd);
		[calView setStringValue:buff];

		}


//	[[monthSelector window] selectCellWithTag:moisInt];



// PrÝsente le mois d'avant le mois courant...

	if (moisInt > 1)  {
		tempMonth = moisInt - 1;
		tempYear = anneeInt;
		}
	else  {
		tempMonth = 12;
		tempYear = anneeInt - 1;
		}

	sprintf( buff, "cal %d %d", tempMonth, tempYear );
	if ( (calCmd=popen(buff, "r")) == NULL )
		[calView1 setStringValue:"Can't build calendar!"];
	else {
		pbuff = buff;
		while( (c=fgetc(calCmd)) != EOF )
			*(pbuff++)=c;
		*pbuff='\0';
		pclose(calCmd);
		[calView1 setStringValue:buff];

		}

// PrÝsente le mois suivant le mois courant...

	if (moisInt < 12)  {
		tempMonth = moisInt + 1;
		tempYear = anneeInt;
		}
	else  {
		tempMonth = 1;
		tempYear = anneeInt + 1;
		}

	sprintf( buff, "cal %d %d", tempMonth, tempYear );
	if ( (calCmd=popen(buff, "r")) == NULL )
		[calView2 setStringValue:"Can't build calendar!"];
	else {
		pbuff = buff;
		while( (c=fgetc(calCmd)) != EOF )
			*(pbuff++)=c;
		*pbuff='\0';
		pclose(calCmd);
		[calView2 setStringValue:buff];

		}

 	[calView display];
 	[calView1 display];
 	[calView2 display];


	[calendarPanel makeKeyAndOrderFront:NULL];

	return self;
}


- calculeDate:sender
{
	long time(), tmpTime;
	struct tm *tm, *localtime();
	tmpTime = time(0);
	tm = localtime( &tmpTime );
	
	jourInt = (tm->tm_mday);
	moisInt = (tm->tm_mon+1);
	anInt = (tm->tm_year);
	anneeInt = (1900+tm->tm_year);
	
	if (moisInt <10)
	  sprintf(mois, "0%d", moisInt);
	else
	  sprintf(mois, "%d", moisInt);

	if (jourInt <10)
	  sprintf(jour, "0%d", jourInt);
	else
	  sprintf(jour, "%d", jourInt);

	sprintf(an, "%d", anInt);

	sprintf(journee, " %d ", jourInt);


	strcat(dateCourante, an);
	strcat(dateCourante, mois);
	strcat(dateCourante, jour);

	return self;
	
}


- moisSuivant:sender
{

	if (moisInt < 12)
		moisInt++;
	else	{
		anneeInt++;
		moisInt = 1;
		}
	
	[self calendar:self];

	return self;
}


- moisPrecedent:sender
{
	if (moisInt > 1)
		moisInt--;
	else	{
		anneeInt--;
		moisInt = 12;
		}
	
	[self calendar:self];

	return self;
}

- selectMonth:sender
{
	moisInt = [[sender selectedCell] tag];
		
	[self calendar:self];

	return self;
}


- selectYear:sender
{
	anneeInt = [yearSelector intValue];

	if (anneeInt > 0 || anneeInt < 10000)  {
		[self calendar:self];
//		[yearSelector setSel:0:0];
		}
	
	return self;
}


- openWindow:sender
{
	[mainWindow makeKeyAndOrderFront:self];

	return self;
}


- controlPad:sender
{
	[self savePad:self];

	activePad = [padSelector selectedTag];

	tempPad = activePad;
	[self selectPadNumber:self];

	[self loadPad:self];

	return self;
}

- selectPadNumber:sender
{

	if (tempPad == 0)
		fichier = "Library/MemoPad/note.memopad.rtfd";

	if (tempPad == 1)
		fichier = "Library/MemoPad/scrap.memopad.rtfd";

	if (tempPad == 2)
		fichier = "Library/MemoPad/reference.memopad.rtfd";

	if (tempPad == 3)
		fichier = "Library/MemoPad/telephone.memopad.rtfd";
		
	if (tempPad == 4)
		fichier = "Library/MemoPad/ftp.memopad.rtfd";
		
	if (tempPad == 5)
		fichier = "Library/MemoPad/stuff.memopad.rtfd";
		
	if (tempPad == 6)
		fichier = "Library/MemoPad/varia.memopad.rtfd";
		
	if (tempPad == 7)
		fichier = "Library/MemoPad/clip.memopad.rtfd";
	

	return self;
}



- loadPad:sender
{

	error = [pad openRTFDFrom:fichier];
	[pad setSel:0:0];
	
	return self;

}


- savePad:sender
{
	
	error = [pad saveRTFDTo:fichier
		   removeBackup:YES
		   errorHandler:self];
		   
	return self;
}

- attemptOverwrite:fichier
{
	NXBeep();

	return self;
}



- ShowInfoPanel:sender
{
  	NXBeep();

	if (!infoPanel)
		[NXApp loadNibSection:"infoPanel.nib" owner:self];
		
	[infoPanel makeKeyAndOrderFront:self];
	
    return self;
}

- showMoreInfoPanel:sender
{
  	NXBeep();

	if (!moreInfoPanel)
		[NXApp loadNibSection:"moreInfo.nib" owner:self];
		
	[moreInfoPanel makeKeyAndOrderFront:self];
	
    return self;
}


- printPad:sender
{

	[pad printPSCode:self];

	return self;
}


- showFindPanel:sender
{
	[findPanel makeKeyAndOrderFront:self];
	[findText selectText:self];

	return self;
}


- findNext:sender
{
	targetText = [findText stringValue];
		
	if ([pad findText:targetText 
               ignoreCase:YES 
                backwards:NO 
                     wrap:YES] == YES);
	
	return self;
}

- findPrevious:sender
{
	targetText = [findText stringValue];
		
	if ([pad findText:targetText
               ignoreCase:YES
                backwards:YES 
                     wrap:YES] == YES);
		
	return self;
}



- clipStuff:(id)pasteboard userData:(const char *)userData error:(char **)msg 
{
	
	[pasteboard types];    // pretend to check the pasteboard types

	temp = [pad textLength]; 
        
	[pad setSel:temp:temp];
	[pad replaceSel:"\n"];

	[pad readSelectionFromPasteboard: pasteboard];

	return self;
}

- autoDate:sender
{

	[pad replaceSel:dateCourante];
	[pad replaceSel:"\n"];

	return self;

}

- autoLine:sender
{

	[pad replaceSel:"ÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐÐ\n"];

	return self;

}

- preference:sender
{

	[preferencePanel makeKeyAndOrderFront:self];
	
    	return self;
}

- getMemoSoundPath:sender
{

	const char *const types[2] = {"snd", NULL};

               	
	if ([[OpenPanel new] runModalForTypes:types] == 1)  
		{ 
		strcpy(memoSound,[[OpenPanel new] filename]);		
		[memoSoundPath setStringValue:memoSound];
		temp = SNDPlaySoundfile(memoSound,5);
		}


	return self;
}

- getVoiceSoundPath:sender
{

	const char *const types[2] = {"snd", NULL};

               	
	if ([[OpenPanel new] runModalForTypes:types] == 1)  
		{ 
		strcpy(voiceSound,[[OpenPanel new] filename]);		
		[voiceSoundPath setStringValue:voiceSound];
		temp = SNDPlaySoundfile(voiceSound,5);
		}


	return self;
}


- saveDefault:sender
{
	out = fopen("Library/MemoPad/memoDefaults", "w");

	fprintf(out,"%d%d%d%d%s\n%s",  memoFlag, voiceFlag, defaultPad, activePad, memoSound, voiceSound );
	
	fclose(out);

	error = [templateTexte saveRTFDTo:"Library/MemoPad/template.rtfd"
		   removeBackup:YES
		   errorHandler:self];
		   
	return self;
}

- playMemoSound:sender
{
	temp = SNDPlaySoundfile(memoSound,5);

	return self;
}

- playVoiceSound:sender
{
	temp = SNDPlaySoundfile(voiceSound,5);

	return self;
}

- controlMemo:sender
{
	memoFlag = [boutonMemo selectedTag];

	if (memoFlag == ON)
		temp = SNDPlaySoundfile(memoSound,5);

	return self;
}

- controlVoice:sender
{
	voiceFlag = [boutonVoice selectedTag];

	if (voiceFlag == ON)		
		temp = SNDPlaySoundfile(voiceSound,5);

	return self;
}


- getDefaultPad:sender
{
	defaultPad = [defaultPadSelector selectedTag];

	return self;
}

- template:sender
{

	// Copie le "template" dans le clipBoard puis vers PAD.

	[templateTexte selectAll:self];
	[templateTexte copy:tempBoard];
	
//	[pad selectAll:self];

	[pad setSel:0:0];

	[pad paste:tempBoard];
	
	[templateTexte selectNull];

	return self;
}

@end
